package com.javaspirit.android.diary.activity;

import java.util.Date;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.javaspirit.android.diary.R;
import com.javaspirit.android.diary.dal.DiaryContentProvider;
import com.javaspirit.android.diary.dal.NoteTable;
import com.javaspirit.android.diary.util.DiaryConstant;

public class WriteNoteFragment extends Fragment{
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    	
        // Inflate the layout for this fragment
    	View root = inflater.inflate(R.layout.writenotefragment, container, false);
    	
    	// MOOD Spinner
        Spinner moodSpinner = (Spinner) root.findViewById(R.id.wrnf_mood_prompt_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getActivity(), R.array.mood_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        moodSpinner.setAdapter(adapter);
    	
        moodSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
			      Toast.makeText(parent.getContext(), "Your mood is " +
			              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				 // Do nothing.
			}
        	
		});
        
        
    	// WEATHER Spinner
        Spinner weatherSpinner = (Spinner) root.findViewById(R.id.wrnf_weather_prompt_spinner);
        ArrayAdapter<CharSequence> weatherAdapter = ArrayAdapter.createFromResource(
                getActivity(), R.array.weather_array, android.R.layout.simple_spinner_item);
        weatherAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        weatherSpinner.setAdapter(weatherAdapter);
    	
        weatherSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
			      Toast.makeText(parent.getContext(), "Your Weather is " +
			              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				 // Do nothing.
			}
        	
		});
        
        
        // Attach event handlers
    	
    	// CANCEL BUTTON
        root.findViewById(R.id.wrnf_b_note_cancel).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	startActivity(new Intent(getActivity(), HomeActivity.class));
            }            
        });
        
        // UPDATE BUTTON
        root.findViewById(R.id.wrnf_b_note_update).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Date today = new Date();
            	Uri url = DiaryContentProvider.CONTENT_URI_NOTE;
            	ContentValues values = new ContentValues();
            	values.put(NoteTable.COLUMN_NOTE, ((EditText)getActivity().findViewById(R.id.wrnf_et_note)).getText().toString());
            	values.put(NoteTable.COLUMN_MOOD, ((Spinner)getActivity().findViewById(R.id.wrnf_mood_prompt_spinner)).getSelectedItemPosition());
            	values.put(NoteTable.COLUMN_WEATHER, ((Spinner)getActivity().findViewById(R.id.wrnf_weather_prompt_spinner)).getSelectedItemPosition());
            	values.put(NoteTable.COLUMN_DATE, DiaryConstant.FORMAT_DATE.format(today));
            	values.put(NoteTable.COLUMN_LAST_MOD_TS, DiaryConstant.FORMAT_TIMESTAMP.format(today));
            	getActivity().getContentResolver().insert(url, values);
            	startActivity(new Intent(getActivity(), HomeActivity.class));
            }            
        });
        
        
    	return root;
    }
    
}
